from myelin.Ns import MYELINS

class Navigator(object):
    
    def __init__(self, locale, dbDriver):
        self.locale = locale
        self.dbDriver = dbDriver
    
    def createParentNav(self, context, node, model):
        defaults = context.server.defaults
        #roots = (
        #    defaults.root_id,
        #    defaults.server_id,
        #)
        roots = defaults.root_id,
            
        p = filter(
            lambda n: n.rdf_value != roots,
            model.mParent
        )
        #p = model.mParent
        if p:
            self.filterNodesByLocale(
                context,
                node,
                p
            )
            
        # if model is a translation of another node,
        # there may exists a parent node in the same locale
        for p in model.mTranslation_Of:
            for p in p.rdf_clone(self.dbDriver).mParent:
                p = p.mTranslation
                if p:
                    self.filterNodesByLocale(
                        context,
                        node,
                        p
                    )
                
        return node
    
    def createGroupingNav(self, context, node, model):
        g = model.mGrouping
        if g:
            self.filterNodesByLocale(
                context,
                node,
                g
            )
        
        for g in model.mTranslation_Of:
            for g in g.mGrouping:
                g = g.mTranslation
                if g:
                    self.filterNodesByLocale(
                        context,
                        node,
                        g
                    )
                
        return node
    
    #def createTranslationNav(self, context, node, model):
    #    results = []
    #    r_a = results.append
    #    def _i18n(n):
    #        if not n.dcLanguage:
    #            return
    #        r_a(n)
    #        for t in n.mTranslation:
    #            _i18n(t)
    #            
    #    for t in model.mTranslation_Of:
    #        _i18n(
    #            t.rdf_clone(
    #                self.dbDriver
    #            )
    #        )
    #        
    #    if not results:
    #        _i18n(
    #            model.rdf_clone(
    #                self.dbDriver
    #            )
    #        )
    #        if len(results) == 1:
    #            return node
    #    
    #    self.createNavNodesSelect(
    #        context,
    #        node,
    #        results,
    #        model
    #    )
    #    return node
    
    def createTranslationNav(self, context, node, model):
        results = model.mTranslation_Of           
        if results:
            return self.createNavNodesSelect(
                context,
                node,
                results,
                model
            )
        return node

    def createNavNodes(self, context, node, models):
        acls = context.acls.checkNodeAcls
        #action = context.action
        map = context.uriMapper.apply
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        for n in models:
            if acls(n.rdf_getDomain(), 'view'):
                n_join = n.rdf_join
                nxa(
                    nxc(
                        u'a:node', MYELINS,
                        attributes={
                            u'node-id'  : n.rdf_value,
                            u'uri'     : map(n.rdf_domain),
                            u'tag'      : n_join('mTag'),
                            u'title'    : n_join('dcTitle'),
                            u'locale'   : n_join('dcLanguage')
                        }
                    )
                )
        return node
    
        
    def filterNodesByLocale(self, context, node, models):
        filtered = []
        fa = filtered.append
        locale = self.locale
        for n in models:
            n = n.rdf_clone(self.dbDriver)
            if locale in n.dcLanguage:
                fa(n)
            else:
                for t in n.mTranslation:
                    if locale in t.dcLanguage:
                        fa(t)
                for t in n.mTranslation_Of:
                    if locale in t.dcLanguage:
                        fa(t)
        return self.createNavNodes(context, node, filtered)
    
    
    def createNavNodesSelect(self, context, node, models, model):
        acls = context.acls.checkNodeAcls
        #action = context.action
        map = context.uriMapper.apply
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        id = model.rdf_value
        
        for n in models:
            if acls(n.rdf_getDomain(), 'view'):
                n_join = n.rdf_join
                if n.rdf_value == id:
                    nxa(
                        nxc(
                            u'a:node', MYELINS,
                            attributes={
                                u'node-id'  : n.rdf_value,
                                u'uri'     : map(n.rdf_domain),
                                u'tag'      : n_join('mTag'),
                                u'title'    : n_join('dcTitle'),
                                u'locale'   : n_join('dcLanguage'),
                                u'selected' : u'1',
                            }
                        )
                    )
                else:
                    nxa(
                        nxc(
                            u'a:node', MYELINS,
                            attributes={
                                u'node-id'  : n.rdf_value,
                                u'uri'     : map(n.rdf_domain),
                                u'tag'      : n_join('mTag'),
                                u'title'    : n_join('dcTitle'),
                                u'locale'   : n_join('dcLanguage')
                            }
                        )
                    )
        return node